﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.App;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.DALFactory;
namespace HKEA.CMS.Core.DAL.App
{
    public class app_tblCPerson_AttributeDAL : Iapp_tblCPerson_Attribute
    {
        HKEA_CMSEntities model;
        app_tblCPerson_Attribute attribute;
        private app_tblCPerson_AttributeDAL()
        {
            model = DataAccess.GetModelInstance();
        }

        public static app_tblCPerson_AttributeDAL GetInstance()
        {
            return new app_tblCPerson_AttributeDAL();
        }

        public int InsertENT(app_tblCPerson_Attribute ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.app_tblCPerson_Attribute.AddObject(ent);
            if (model.SaveChanges() > 0)
                return model.app_tblCPerson_Attribute.Max(a => a.AppCPerson_AttributeUID);
            return 0;
        }
        public int InsertList(List<app_tblCPerson_Attribute> list)
        {
            foreach (var ent in list)
            {
                ent.CreationDate = DateTime.Now;
                ent.LastModifyDate = DateTime.Now;
                model.app_tblCPerson_Attribute.AddObject(ent);
            }
            return model.SaveChanges();
        }

        public List<app_tblCPerson_Attribute> GetListByENT(app_tblCPerson_Attribute ent, bool AddDeletedForSelect)
        {
            var ret = model.app_tblCPerson_Attribute.Where(a => a.AppCPerson_AttributeUID > 0);
            if (AddDeletedForSelect)
                ret = ret.Where(a => a.Deleted == ent.Deleted);
            if (ent.AppContactPersonUID > 0)
                ret = ret.Where(a => a.AppContactPersonUID == ent.AppContactPersonUID);
            if (ent.ContactPersonUID > 0)
                ret = ret.Where(a => a.ContactPersonUID == ent.ContactPersonUID);
            return ret.ToList();
        }

        public int GetCountByENT(app_tblCPerson_Attribute ent)
        {
            throw new NotImplementedException();
        }

        public int UpdateENT(app_tblCPerson_Attribute ent)
        {
            var ret = model.app_tblCPerson_Attribute.Where(a => a.AppCPerson_AttributeUID == ent.AppCPerson_AttributeUID).FirstOrDefault();
            if (ret != null)
            {
                ret.AppContactPersonUID = ent.AppContactPersonUID;
                ret.AttributeUID = ent.AttributeUID;
                ret.ContactPersonUID = ent.ContactPersonUID;
                ret.CPerson_AttributeUID = ent.CPerson_AttributeUID;
                ret.Deleted = ent.Deleted;
                ret.LastModifyDate = DateTime.Now;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(app_tblCPerson_Attribute ent)
        {
            var ret = model.app_tblCPerson_Attribute.Where(a => a.AppCPerson_AttributeUID == ent.AppCPerson_AttributeUID).FirstOrDefault();
            if (ret != null)
            {
                model.app_tblCPerson_Attribute.DeleteObject(ret);
            }
            return model.SaveChanges();
        }


        public int DeleteByKeyList(List<int> list)
        {
            foreach (var temp in list)
            {
                var result = model.app_tblCPerson_Attribute.Where(a => a.AppCPerson_AttributeUID == temp).FirstOrDefault();
                if (result != null)
                    model.app_tblCPerson_Attribute.DeleteObject(result);
            }
            return model.SaveChanges();
        }

        public int SaveList(List<int> ToDeletedKey, List<app_tblCPerson_Attribute> AddList, List<app_tblCPerson_Attribute> AddToDeletedList)
        {
            foreach (var temp in ToDeletedKey)
            {
                var result = model.app_tblCPerson_Attribute.Where(a => a.AppCPerson_AttributeUID == temp).FirstOrDefault();
                if (result != null)
                    model.app_tblCPerson_Attribute.DeleteObject(result);
            }
            foreach (var temp in AddList)
            {
                temp.CreationDate = DateTime.Now;
                temp.LastModifyDate = DateTime.Now;
                model.app_tblCPerson_Attribute.AddObject(temp);
            }
            foreach (var temp in AddToDeletedList)
            {
                temp.CreationDate = DateTime.Now;
                temp.LastModifyDate = DateTime.Now;
                model.app_tblCPerson_Attribute.AddObject(temp);
            }
            return model.SaveChanges();
        }
    }
}
